﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ProgNetComponents.Validation.Rules
{
    /// <summary>
    /// Rule checks if all columns are not null
    /// </summary>
    public class MultiColumnsRequiredValidationRule : MultiColumnsValidationRule, IStopOnFirstErrorValidationRule
    {
        #region Static
        /// <summary>
        /// Initializes the <see cref="MultiColumnsRequiredValidationRule"/> class.
        /// </summary>
        static MultiColumnsRequiredValidationRule()
        {
            GlobalDefaultErrorString = "Ta wartość jest wymagana.";
        }
        /// <summary>
        /// Gets or sets the global default error string.
        /// </summary>
        /// <value>
        /// The global default error string.
        /// </value>
        public static string GlobalDefaultErrorString { get; set; }
        #endregion

        #region Properties
        /// <summary>
        /// Gets default error string
        /// </summary>
        /// <value>
        /// The default error string.
        /// </value>
        protected override string DefaultErrorString
        {
            get
            {
                return GlobalDefaultErrorString;
            }
        }

        /// <summary>
        /// Gets or sets if validation should stop on first fail
        /// </summary>
        /// <value>
        ///   <c>true</c> if [stop on first error]; otherwise, <c>false</c>.
        /// </value>
        public bool StopOnFirstError
        {
            get;
            set;
        }
        #endregion
        /// <summary>
        /// Initializes a new instance of the <see cref="MultiColumnsRequiredValidationRule"/> class.
        /// </summary>
        public MultiColumnsRequiredValidationRule() { }

        /// <summary>
        /// Initializes a new instance of the <see cref="MultiColumnsRequiredValidationRule"/> class.
        /// </summary>
        /// <param name="columns">The columns.</param>
        /// <param name="error">The error.</param>
        public MultiColumnsRequiredValidationRule(string[] columns, string error = null)
            : base(columns, error)
        {
            
        }

        /// <summary>
        /// Does the validate.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <returns></returns>
        protected override bool DoValidate(DataRow row)
        {
            bool result = true;
            foreach (string column in Columns)
            {
                if (row.GetColumnValue(column).IsNull())
                {
                    row.SetColumnError(column, FormatedErrorString);
                    result = false;
                    if (StopOnFirstError)
                        break;
                }
            }
            return result;
        }

        /// <summary>
        /// Called when [validation failed].
        /// </summary>
        /// <param name="row">The row.</param>
        protected override void OnValidationFailed(DataRow row)
        {
            
        }
    }
}
